package com.kc.gb.device.plugins.ptl.controller;

import ATOPSTD.TagRcvMsg;
import com.alibaba.fastjson.JSON;
import com.kc.gb.base.dto.Result;
import com.kc.gb.device.common.utils.SpringHelper;
import com.kc.gb.device.plugins.ptl.events.support.PtlPublishEvent;
import com.kc.gb.device.plugins.ptl.service.PtlHardwareService;
import com.kc.gb.feign.ptl.support.dtos.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
@RestController
@RequestMapping("/device/ptl")
public class PtlHardwareController {

    @Resource
    PtlHardwareService iDevicePtlService;

    @PostMapping("/commands")
    <T extends AbstractPtlCommand> Result<?> commands(@RequestBody List<T> commands) {
        // 硬件设备直接调用默认使用模拟器模式
        AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);
        iDevicePtlService.consumeCommands(commands,DEBUG_MODE.get());
        return Result.success();
    }

    @PostMapping("/light")
    Result<?> light(@RequestBody LightCommand body){
        // 硬件设备直接调用默认使用模拟器模式
        AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);
        iDevicePtlService.consumeCommands(Arrays.asList(body),DEBUG_MODE.get());
        return Result.success();
    }

    @PostMapping("/led")
    Result<?>  led(@RequestBody LedCommand body){
        // 硬件设备直接调用默认使用模拟器模式
        AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);
        iDevicePtlService.consumeCommands(Arrays.asList(body),DEBUG_MODE.get());
        return Result.success();
    }

    @PostMapping("/button")
    Result<?>  button(@RequestBody ButtonCommand body){
        // 硬件设备直接调用默认使用模拟器模式
        AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);
        iDevicePtlService.consumeCommands(Arrays.asList(body),DEBUG_MODE.get());
        return Result.success();
    }

    @PostMapping("/beep")
    Result<?>  beep(@RequestBody BeepCommand body){
        // 硬件设备直接调用默认使用模拟器模式
        AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);
        iDevicePtlService.consumeCommands(Arrays.asList(body),DEBUG_MODE.get());
        return Result.success();
    }

    @PostMapping("/feedback")
    Result<?>  feedback(@RequestParam int lightNo,@RequestParam int subcmd){
        TagRcvMsg tagRcvMsg = new TagRcvMsg();
        tagRcvMsg.nodeaddr = (short)lightNo;
        tagRcvMsg.subcmd = (short)subcmd;
        log.info("PTL feedback:{}", JSON.toJSONString(tagRcvMsg));
        SpringHelper.getApplicationContext().publishEvent(new PtlPublishEvent(tagRcvMsg));
        return Result.success();
    }
}
